﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace boe2.irc
{
	public class IrcChannel : DependencyObject
	{
		public String Topic { get; private set; }
		public String Name { get; private set; }
		public Boolean Alerted { get; set; }

		public ObservableCollection<IrcMessage> Messages
		{
			get { return (ObservableCollection<IrcMessage>)GetValue(MessagesProperty); }
			set { SetValue(MessagesProperty, value); }
		}
		public static readonly DependencyProperty MessagesProperty =
			DependencyProperty.Register("Messages", typeof(ObservableCollection<IrcMessage>), typeof(IrcChannel), new UIPropertyMetadata());

		public ObservableCollection<IrcUser> Users
		{
			get { return (ObservableCollection<IrcUser>)GetValue(UsersProperty); }
			set { SetValue(UsersProperty, value); }
		}
		public static readonly DependencyProperty UsersProperty =
			DependencyProperty.Register("Users", typeof(ObservableCollection<IrcUser>), typeof(IrcChannel), new PropertyMetadata());

		

		public bool Equals(IrcChannel channel)
		{
			return this.Name == channel.Name;
		}

		public IrcChannel(String name)
		{
			Alerted = false;
			Name = name;
			Messages = new ObservableCollection<IrcMessage>();
			Users = new ObservableCollection<IrcUser>();
		}

		public void AddMessage(IrcMessage message)
		{
			Messages.Add(message);
		}

		public void SetTopic(String topic)
		{
			Topic = topic;
		}

		public void AddUser(String user)
		{
			if (!Users.Select(iUsr => iUsr.Nick).Contains(user))
			{
				Users.Add(new IrcUser(user));
			}
		}

		public void RemoveUser(String user)
		{
			IrcUser iUser = Users.Where(iUsr => iUsr.Nick == user).FirstOrDefault();
			if (iUser != null)
			{
				Users.Remove(iUser);
			}
		}
	}
}
